﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmDuyetPhieuXuat : Form
    {
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private KhoManagement khoMgt = new KhoManagement();
        private QuanLyKhoManagement quanlykhoMgt = new QuanLyKhoManagement();
        private ButToanMgt buttoanMgt = new ButToanMgt();

        public bool isNguoiKiemTra = false;
        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmDuyetPhieuXuat()
        {
            InitializeComponent();
        }

        public void LoadAll()
        {
            //neu la ke toan truong
            if (!isNguoiKiemTra)
            {
                btnDu.Enabled = false;
                btnInPKT.Enabled = false;
                colKeToanTruong.Visible = true;
                colNgayChungTu.Visible = true;
                dgvDuyetPhieuXuatHang.DataSource = GetAllNewPhieuXuatHangKeToanTruong();
            }
            //neu la nguoi kiem tra hang
            else if (isNguoiKiemTra)
            {
                btnXacNhan.Enabled = false;
                btnInPX.Enabled = false;
                dgvDuyetPhieuXuatHang.DataSource = GetAllNewPhieuXuatHangNguoiKiemTraHang();
            }

            for (int i = 0; i < dgvDuyetPhieuXuatHang.Rows.Count; i++)
            {
                string sDate6 = DateTime.Parse(dgvDuyetPhieuXuatHang.Rows[i].Cells[6].Value.ToString()).ToShortDateString();
                string sDate20 = DateTime.Parse(dgvDuyetPhieuXuatHang.Rows[i].Cells[20].Value.ToString()).ToShortDateString();
                if (sDate6 == "1/1/1753")
                    dgvDuyetPhieuXuatHang.Rows[i].Cells[6].Value = "";
                if(sDate20 == "1/1/1753")
                    dgvDuyetPhieuXuatHang.Rows[i].Cells[20].Value = "";
            }

            if (!isNguoiKiemTra)
            {
                if (dgvDuyetPhieuXuatHang.Rows.Count > 0 && dgvDuyetPhieuXuatHang.Rows[0].Cells[19].Value.ToString() == "")
                    btnInPX.Enabled = false;
                else
                    btnInPX.Enabled = true;
            }
        }

        private void FrmDuyetPhieuXuat_Load(object sender, EventArgs e)
        {            
            dgvDuyetPhieuXuatHang.AutoGenerateColumns = false;
            cboChiNhanhCty.DataSource = GetAllChiNhanhCongTy();
            cboChiNhanhCty.DisplayMember = "TenChiNhanh";
            cboChiNhanhCty.ValueMember = "Id";
            cboChiNhanhCty.SelectedIndex = 0;

            LoadAll();            
        }

        private void dgvDuyetPhieuXuatHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                //neu nguoi kiem tra hang empty
                if (dgvDuyetPhieuXuatHang.CurrentRow.Cells[5].Value.ToString() != "")
                {
                    if (dgvDuyetPhieuXuatHang.CurrentRow.Cells[19].Value.ToString() != "")
                    {
                        //var lst = (from p in GetAllButToan()
                        //           where p.SoChungTu == dgvDuyetPhieuXuatHang.CurrentRow.Cells[0].Value.ToString()
                        //           select p);
                        btnXacNhan.Enabled = false;
                        btnInPX.Enabled = true;
                    }
                    else
                    {
                        btnXacNhan.Enabled = true;
                        btnInPX.Enabled = false;
                    }
                }
                else
                {
                    btnXacNhan.Enabled = false;
                    btnInPX.Enabled = false;
                }
            }
           
        }      

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadAll();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            int idChiNhanhCty = -1;
            if (cboChiNhanhCty.SelectedItem != null)
                idChiNhanhCty = ((ChiNhanhCongTy)cboChiNhanhCty.SelectedItem).Id;
            List<PhieuXuatHang> lstPhieuXuatHang = (from pxh in GetAllPhieuXuatHang()
                                                    where (pxh.MaSo.Contains(txtTimKiem.Text) ||
                                                      pxh.NguoiKiemTraHang.Contains(txtTimKiem.Text) ||
                                                      pxh.KeToanTruong.Contains(txtTimKiem.Text)) &&
                                                      pxh.IdChiNhanhCongTy == idChiNhanhCty &&
                                                      pxh.NgayThang.Date >= dtTuNgay.Date && pxh.NgayThang.Date <= dtDenNgay.Date
                                                      select pxh).ToList();
            if (this.nhanvien.IdChucVu == 3)
            {
                btnDu.Enabled = false;
                btnInPKT.Enabled = false;
                dgvDuyetPhieuXuatHang.DataSource = lstPhieuXuatHang;
            }
            //neu la nguoi kiem tra hang
            else if (this.nhanvien.IdChucVu == 4)
            {
                btnXacNhan.Enabled = false;
                btnInPX.Enabled = false;
                List<PhieuXuatHang> listPhieuXuatHang1 = (from pxh in lstPhieuXuatHang
                                                          where pxh.NguoiKiemTraHang == "" && pxh.NgayKiemTraHang.Date == DateTime.Parse("1/1/1753")
                                                         select pxh).ToList();
                dgvDuyetPhieuXuatHang.DataSource = listPhieuXuatHang1;
            }
            for (int i = 0; i < dgvDuyetPhieuXuatHang.Rows.Count; i++)
            {
                string sDate = DateTime.Parse(dgvDuyetPhieuXuatHang.Rows[i].Cells[6].Value.ToString()).ToShortDateString();
                if (sDate == "1/1/1753")
                    dgvDuyetPhieuXuatHang.Rows[i].Cells[6].Value = "";
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                if (dgvDuyetPhieuXuatHang.CurrentRow.Cells[5].Value.ToString() != "")
                {
                    ResultObj resultObj = new ResultObj();

                    PhieuXuatHang phieuxuathang = new PhieuXuatHang();
                    phieuxuathang.MaSo = dgvDuyetPhieuXuatHang.CurrentRow.Cells[0].Value.ToString();
                    phieuxuathang.NgayThang = DateTime.Parse(dgvDuyetPhieuXuatHang.CurrentRow.Cells[1].Value.ToString());
                    phieuxuathang.KhachHang = dgvDuyetPhieuXuatHang.CurrentRow.Cells[2].Value.ToString();
                    phieuxuathang.DiaChi = dgvDuyetPhieuXuatHang.CurrentRow.Cells[3].Value.ToString();
                    phieuxuathang.IdChiNhanhCongTy = Convert.ToInt32(dgvDuyetPhieuXuatHang.CurrentRow.Cells[4].Value.ToString());
                    phieuxuathang.NguoiKiemTraHang = dgvDuyetPhieuXuatHang.CurrentRow.Cells[5].Value.ToString();
                    phieuxuathang.NgayKiemTraHang = DateTime.Parse(dgvDuyetPhieuXuatHang.CurrentRow.Cells[6].Value.ToString());
                    phieuxuathang.Id = Convert.ToInt32(dgvDuyetPhieuXuatHang.CurrentRow.Cells[7].Value.ToString());
                    phieuxuathang.ChiNhanhKhachHang = dgvDuyetPhieuXuatHang.CurrentRow.Cells[8].Value.ToString();
                    phieuxuathang.MaSoThue = dgvDuyetPhieuXuatHang.CurrentRow.Cells[9].Value.ToString();
                    phieuxuathang.HinhThucThanhToan = dgvDuyetPhieuXuatHang.CurrentRow.Cells[10].Value.ToString();
                    phieuxuathang.NguoiLap = dgvDuyetPhieuXuatHang.CurrentRow.Cells[11].Value.ToString();
                    phieuxuathang.TongTienHang = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[12].Value.ToString());
                    phieuxuathang.VAT = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[13].Value.ToString());
                    phieuxuathang.TienVAT = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[14].Value.ToString());
                    phieuxuathang.ChietKhau = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[15].Value.ToString());
                    phieuxuathang.TienChietKhau = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[16].Value.ToString());
                    phieuxuathang.TongCong = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[17].Value.ToString());
                    phieuxuathang.SoHoaDonDo = dgvDuyetPhieuXuatHang.CurrentRow.Cells[18].Value.ToString();
                    phieuxuathang.KeToanTruong = this.nhanvien.HoTen;
                    phieuxuathang.NgayChungTu = DateTime.Parse(DateTime.Now.ToLongDateString());
                    resultObj = phieuxuathangMgt.UpdatePhieuXuatHang(phieuxuathang);

                    if (resultObj.ReturnCode > 0)
                    {
                        MessageBox.Show("Cập nhật thành công", "Thông báo");
                        LoadAll();
          
                    }
                    else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                        MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    else
                        MessageBox.Show("Thất bại");
                }
            }
        }

        private void btnDu_Click(object sender, EventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                ResultObj resultObj = new ResultObj();

                PhieuXuatHang phieuxuathang = new PhieuXuatHang();
                phieuxuathang.MaSo = dgvDuyetPhieuXuatHang.CurrentRow.Cells[0].Value.ToString();
                phieuxuathang.NgayThang = DateTime.Parse(dgvDuyetPhieuXuatHang.CurrentRow.Cells[1].Value.ToString());
                phieuxuathang.KhachHang = dgvDuyetPhieuXuatHang.CurrentRow.Cells[2].Value.ToString();
                phieuxuathang.DiaChi = dgvDuyetPhieuXuatHang.CurrentRow.Cells[3].Value.ToString();
                phieuxuathang.IdChiNhanhCongTy = Convert.ToInt32(dgvDuyetPhieuXuatHang.CurrentRow.Cells[4].Value.ToString());
                phieuxuathang.NguoiKiemTraHang = this.nhanvien.HoTen;
                phieuxuathang.NgayKiemTraHang = DateTime.Now.Date;
                phieuxuathang.Id = Convert.ToInt32(dgvDuyetPhieuXuatHang.CurrentRow.Cells[7].Value.ToString());
                phieuxuathang.ChiNhanhKhachHang = dgvDuyetPhieuXuatHang.CurrentRow.Cells[8].Value.ToString();
                phieuxuathang.MaSoThue = dgvDuyetPhieuXuatHang.CurrentRow.Cells[9].Value.ToString();
                phieuxuathang.HinhThucThanhToan = dgvDuyetPhieuXuatHang.CurrentRow.Cells[10].Value.ToString();
                phieuxuathang.NguoiLap = dgvDuyetPhieuXuatHang.CurrentRow.Cells[11].Value.ToString();
                phieuxuathang.TongTienHang = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[12].Value.ToString());
                phieuxuathang.VAT = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[13].Value.ToString());
                phieuxuathang.TienVAT = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[14].Value.ToString());
                phieuxuathang.ChietKhau = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[15].Value.ToString());
                phieuxuathang.TienChietKhau = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[16].Value.ToString());
                phieuxuathang.TongCong = Convert.ToInt64(dgvDuyetPhieuXuatHang.CurrentRow.Cells[17].Value.ToString());
                phieuxuathang.SoHoaDonDo = dgvDuyetPhieuXuatHang.CurrentRow.Cells[18].Value.ToString();
                phieuxuathang.NgayChungTu = DateTime.Parse("1/1/1753");
                resultObj = phieuxuathangMgt.UpdatePhieuXuatHang(phieuxuathang);

                if (resultObj.ReturnCode > 0)
                {
                    MessageBox.Show("Cập nhật thành công", "Thông báo");
                    LoadAll();
                }
                else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
        }

        private void btnInPX_Click(object sender, EventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvDuyetPhieuXuatHang.CurrentRow.DataBoundItem;

                FrmPhieuXuatHang frmPhieuXuatHang = new FrmPhieuXuatHang();
                frmPhieuXuatHang.fromClosePhieuXuatHang += new FormClosePhieuNhapKhoEventHandler(frmPhieuXuatHang_fromClosePhieuXuatHang);
                frmPhieuXuatHang.NhanVien = this.nhanvien;
                frmPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                frmPhieuXuatHang.IsEdited = true;
                frmPhieuXuatHang.isPrinting = true;
                frmPhieuXuatHang.Show();
            }
            else
            {
                MessageBox.Show("Phiếu không in được");
            }
        }

        void frmPhieuXuatHang_fromClosePhieuXuatHang()
        {
            btnTimKiem_Click(null, null);
        }

        private void dgvDuyetPhieuXuatHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvDuyetPhieuXuatHang.CurrentRow.DataBoundItem;
                FrmKiemTraPhieuXuatHang frmKiemTraPhieuXuatHang = new FrmKiemTraPhieuXuatHang();
                frmKiemTraPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                frmKiemTraPhieuXuatHang.Show();
            }
        }

        private void btnInPKT_Click(object sender, EventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvDuyetPhieuXuatHang.CurrentRow.DataBoundItem;
                FrmXemReport frmXemReport = new FrmXemReport(phieuxuathang);
                frmXemReport.Show();
            }
        }

        public List<PhieuXuatHang> GetAllNewPhieuXuatHangKeToanTruong()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> listPhieuXuatHang = (from pxh in (List<PhieuXuatHang>)resultObj.Result
                                                     where pxh.IsDelete == 0
                                                     select pxh).ToList();
                                                     
            return listPhieuXuatHang;
        }

        public List<PhieuXuatHang> GetAllNewPhieuXuatHangNguoiKiemTraHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> listPhieuXuatHang = (from pxh in (List<PhieuXuatHang>)resultObj.Result
                                                     where pxh.NguoiKiemTraHang == "" && (pxh.NgayKiemTraHang.Date == DateTime.Parse("1/1/1753") || pxh.NgayKiemTraHang.Date == DateTime.Parse("1/1/0001"))
                                                     && pxh.IsDelete == 0
                                                     select pxh).ToList();
            return listPhieuXuatHang;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                                           where cn.IsDelete==0
                                                           select cn).ToList();
            return listChiNhanhCongTy;
        }

        public List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> listPhieuXuatHang = (from pxh in (List<PhieuXuatHang>)resultObj.Result
                                                     where pxh.IsDelete == 0
                                                     select pxh).ToList();
            return listPhieuXuatHang;
        }

        public List<CTPhieuXuatHang> GetAllCTPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> listCTPhieuXuatHang = (from ctpx in (List<CTPhieuXuatHang>)resultObj.Result
                                                         where ctpx.IsDelete == 0
                                                         select ctpx).ToList();
            return listCTPhieuXuatHang;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> listHangThanhPham = (from hh in (List<HangHoa>)resultObj.Result
                                               where hh.IsDelete == 0
                                               select hh).ToList();

            return listHangThanhPham;
        }

        public List<Kho> GetAllKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khoMgt.GetAllKho();
            List<Kho> listKho = (List<Kho>)resultObj.Result;

            return listKho;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<QuanLyKho> GetAllQuanLyKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = quanlykhoMgt.GetAllQuanLyKho();
            List<QuanLyKho> listQuanLyKho = (from ql in(List<QuanLyKho>)resultObj.Result
                                                 where ql.IsDelete==0
                                                 select ql).ToList();
                                             

            return listQuanLyKho;
        }

        private void dgvDuyetPhieuXuatHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
